package com.rxnetwork.sample.mvp.toolbar

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout

import com.rxnetwork.sample.R
import com.rxnetwork.sample.utils.StatusBarUtil

import me.imid.swipebacklayout.lib.SwipeBackLayout

/**
 * by xiaocode
 * 带标题栏的activity类
 */
abstract class BaseTopBarActivity : BaseSwipeBackActivity() {

    var topBarLayout: TopbarLayout? = null
        private set

    /**
     * 便于设置全局崩溃，网络错误，空白页

     * @param contentLayout
     */
    var contentLayout: FrameLayout? = null

    // 右滑返回
    private var mSwipeBackLayout: SwipeBackLayout? = null

    private //处理android 4.4以下会崩溃
    val contentViewLayout: View
        get() {
            val view = View.inflate(this, R.layout.activity_top_bar, null)
            topBarLayout = view.findViewById(R.id.top_bar_layout) as TopbarLayout
            topBarLayout?.setTitle(topTitle)
            try {
                StatusBarUtil.setColor(this, if (topBarLayout?.topbarBgColor as Int > 0)
                    topBarLayout?.topbarBgColor as Int
                else
                    resources.getColor(android.R.color.white), 0)
            } catch (e: Exception) {
            }

            contentLayout = view.findViewById(R.id.content_layout) as FrameLayout
            if (contentViewResId > 0) {
                View.inflate(this, contentViewResId, contentLayout)
            }
            return view
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (contentViewResId != 0) {
            setContentView(contentViewLayout)
        }
        try {
            setTopBarLayoutColor(resources.getColor(android.R.color.white))
        } catch (e: Exception) {
        }

        initSwipeBack()
        initPresenter()
        initViewsAndEvents()
    }

    protected fun initSwipeBack() {
        mSwipeBackLayout = swipeBackLayout
        mSwipeBackLayout?.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mSwipeBackLayout?.addSwipeListener(object : SwipeBackLayout.SwipeListener {
                override fun onScrollStateChange(state: Int, scrollPercent: Float) {
                    if (state == 0) {
                        //设置颜色，便于状态栏文字颜色 设置回来
                        setTopBarLayoutColor(topBarLayout?.topbarBgColor as Int)
                    }
                }

                override fun onEdgeTouch(edgeFlag: Int) {
                    StatusBarUtil.setTranslucent(this@BaseTopBarActivity)
                    StatusBarUtil.setStateBarColor(this@BaseTopBarActivity, topBarLayout?.topbarBgColor as Int)
                    //设置一遍margin 界面才会刷新，不解？
                    val params = topBarLayout?.layoutParams as ViewGroup.MarginLayoutParams
                    topBarLayout?.layoutParams = params
                }

                override fun onScrollOverThreshold() {}
            })
        }
    }

    override fun startActivity(intent: Intent) {
        super.startActivity(intent)
        overridePendingTransition(R.anim.in_right, R.anim.out_left)
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.in_left, R.anim.out_right)
    }

    fun setTopBarLayoutColor(color: Int) {
        topBarLayout?.topbarBgColor = color
        StatusBarUtil.setColor(this, color, 0)
    }

    protected abstract fun initPresenter()

    protected abstract fun initViewsAndEvents()

    protected abstract val contentViewResId: Int

    protected abstract val topTitle: CharSequence

}
